from PyQt5.QtWidgets import QApplication, QDialog, QVBoxLayout, QLabel, QLineEdit, QPushButton, QSizePolicy, \
    QHBoxLayout, QMenu, QAction, QWidget
from PyQt5.QtCore import Qt, QPointF
from PyQt5.QtGui import QPainter, QPen, QColor



class CustomInputDialog(QDialog):
    def __init__(self, parent=None):
        super(CustomInputDialog, self).__init__(parent)
        self.setWindowFlags(Qt.FramelessWindowHint)  # 设置无边框窗口
        self.setFixedSize(300, 150)
        self.setStyleSheet("background-color: #f0f0f0; border-radius: 10px;")

        self.layout = QVBoxLayout(self)

        self.label = QLabel(self)
        self.label.setStyleSheet("color: #333333; font-size: 14px;")
        self.layout.addWidget(self.label)

        # 添加一个弹性空间以使输入框位于垂直中央
        self.layout.addStretch(1)

        self.line_edit = QLineEdit(self)
        self.line_edit.setStyleSheet("border: 1px solid #cccccc; border-radius: 5px; padding: 5px; font-size: 14px;")
        self.layout.addWidget(self.line_edit)

        # 再添加一个弹性空间以在底部保持间距
        self.layout.addStretch(1)

        button_layout = QHBoxLayout()

        self.ok_button = QPushButton("OK", self)
        self.ok_button.setStyleSheet("background-color: #007acc; color: white; border-radius: 5px; padding: 5px; font-size: 14px;")
        self.ok_button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
        button_layout.addWidget(self.ok_button)

        self.cancel_button = QPushButton("Cancel", self)
        self.cancel_button.setStyleSheet("background-color: #cccccc; color: #333333; border-radius: 5px; padding: 5px; font-size: 14px;")
        self.cancel_button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
        button_layout.addWidget(self.cancel_button)

        self.layout.addLayout(button_layout)

        self.ok_button.clicked.connect(self.accept)
        self.cancel_button.clicked.connect(self.reject)

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)
        painter.setPen(QPen(QColor("#cccccc"), 1))
        painter.setBrush(QColor("#f0f0f0"))
        painter.drawRoundedRect(self.rect(), 10, 10)

    def getText(self, label):
        self.label.setText(label)
        if self.exec_() == QDialog.Accepted:
            return self.line_edit.text(), True
        return "", False


if __name__ == "__main__":
    app = QApplication([])
    dialog = CustomInputDialog()
    if dialog.exec_() == QDialog.Accepted:
        print("Input:", dialog.line_edit.text())
    app.exec_()
